#!/bin/bash

if [ ! -f util/.serverpod_util_root ]; then
    echo "Run this script from the root of the Serverpod repository"
    echo "I.e. util/run_tests_update_pubspecs"
    exit 1
fi

# Makes script exit on first non-zero error code
set -e

echo "### Run serverpod generate test"
echo "### If this test fails, make sure that you have run serverpod generate"
echo "### on all packages with util/generate_all"
echo "### Also check that dependency versions are consistent across packages."

# Install the serverpod command
echo "### Installing CLI tools"

export PATH="$PATH":"$HOME/.pub-cache/bin"

export SERVERPOD_HOME=$(pwd)
echo "### Serverpod home: $SERVERPOD_HOME"

# Verify that template directory exists
TEMPLATE_DIR="$SERVERPOD_HOME/templates/serverpod_templates"
echo "### Templates at: $TEMPLATE_DIR"
test -d $TEMPLATE_DIR

cd tools/serverpod_cli
dart pub get
dart pub global activate -s path .
cd ../..

util/update_pubspecs

util/ensure_no_changes

echo "### Analyze pubspecs"
serverpod analyze-pubspecs

echo "### Test complete"
